//
//  UIView+Extension.swift
//  NetWork
//
//  Created by Delin Meng 孟德林 on 2018/9/21.
//  Copyright © 2018年 Delin Meng 孟德林. All rights reserved.
//

import UIKit
import SnapKit

extension UIView {

    static func loadNib() -> Self {
        func loadNib<T: UIView>(_ viewType: T.Type) -> T {
            let bundle = Bundle(for: self)
            let name = String(describing: self)

            return bundle.loadNibNamed(name, owner: nil, options: nil)?.first as! T
        }

        return loadNib(self)
    }


    func sizeContainer() {
        superview?.snp.updateConstraints { (make) in
            let height = self.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
            make.height.equalTo(height)
        }
    }

//    func toggleVisible(subview: UIView) {
//        subviews.forEach { (aView) in
//            aView.isHidden = aView != subview
//
//            if aView == subview {
//                let height = subview.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
//                self.snp.updateConstraints { (make) in
//                    make.height.equalTo(height)
//                }
//            }
//        }
//    }


}

extension UIView {


    /// 给视图的layer层添加圆角和阴影
    ///
    /// - Parameters:
    ///   - cornerRadius: 圆角
    ///   - backgroudLayerColor: view 背景颜色
    ///   - shadowColor: 阴影背景颜色
    ///   - shadowOpacity: 阴影透明度
    ///   - shadowRadius: 圆角
    func addShadowWithRoundedCorners(cornerRadius:CGFloat? = 5,
                                     backgroudLayerColor:UIColor? = UIColor.white,
                                     shadowColor:UIColor? = UIColor.black,
                                     shadowOpacity:CGFloat? = 0.06,
                                     shadowOffset:CGSize = CGSize.zero,
                                     shadowRadius:CGFloat?) {

        self.layer.cornerRadius = cornerRadius ?? 5
        self.layer.backgroundColor = backgroudLayerColor?.cgColor ?? UIColor.white.cgColor
        self.layer.shadowColor = shadowColor?.cgColor
        self.layer.shadowOffset = shadowOffset // CGSize(width: 0, height: 1)
        self.layer.shadowOpacity = Float(shadowOpacity ?? 0.06)
        if shadowRadius != nil {
            self.layer.shadowRadius = shadowRadius ?? 5
        }
    }

    // 拍摄快照图片
    func snapshot(completion:@escaping (_ signImage:UIImage)->()) {
        let size = CGSize(width: self.bounds.size.width, height: self.bounds.size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale * 2)  //
        let context = UIGraphicsGetCurrentContext()!
        layer.drawsAsynchronously = true
        layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        guard let siimage = image else {
            completion(UIImage())
            return
        }
        completion(siimage)
        UIGraphicsEndImageContext()
    }
}


extension UIView {


    func firstViewController() -> UIViewController? {
        for view in sequence(first: self.superview, next: { $0?.superview }) {
            if let responder = view?.next {
                if responder.isKind(of: UIViewController.self){
                    return responder as? UIViewController
                }
            }
        }
        return nil
    }

}


extension UIView {

    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }

        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }

    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue > 0 ? newValue : 0
        }
    }

    @IBInspectable var borderColor: UIColor {
        get {
            return UIColor(cgColor: layer.borderColor!)
        }
        set {
            layer.borderColor = newValue.cgColor
        }
    }

    @IBInspectable var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }

    @IBInspectable var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }

    @IBInspectable var shadowOpacity: CGFloat {
        get {
            return CGFloat(layer.shadowOpacity)
        }
        set {
            layer.shadowOpacity = Float(newValue)
        }
    }

    @IBInspectable var shadowColor: UIColor {
        get {
            return UIColor(cgColor: layer.shadowColor!)
        }
        set {
            layer.shadowColor = newValue.cgColor
        }
    }


}


extension UIView {


    func setX(_ x: CGFloat) {
        var frame: CGRect = self.frame
        frame.origin.x = x
        self.frame = frame
    }

    func setY(_ y: CGFloat) {
        var frame: CGRect = self.frame
        frame.origin.y = y
        self.frame = frame
    }

    func x() -> CGFloat {
        return frame.origin.x
    }

    func y() -> CGFloat {
        return frame.origin.y
    }
    func setWidth(_ width: CGFloat) {
        var frame: CGRect = self.frame
        frame.size.width = width
        self.frame = frame
    }
    func width() -> CGFloat {
        return frame.size.width
    }
    func setHeight(_ height: CGFloat) {
        var frame: CGRect = self.frame
        frame.size.height = height
        self.frame = frame
    }
    func height() -> CGFloat {
        return frame.size.height
    }
    func setCenterX(_ centerX: CGFloat) {
        var center: CGPoint = self.center
        center.x = centerX
        self.center = center
    }
    func centerX() -> CGFloat {
        return center.x
    }
    func setCenterY(_ centerY: CGFloat) {
        var center: CGPoint = self.center
        center.y = centerY
        self.center = center
    }
    func centerY() -> CGFloat {
        return center.y
    }
    func setSize(_ size: CGSize) {
        var frame: CGRect = self.frame
        frame.size = size
        self.frame = frame
    }
    func size() -> CGSize {
        return frame.size
    }

}
